package com.hengpeng.itfinoss.service.activity.impl;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.hengpeng.common.exception.ResourceNotFoundException;
import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.QueryResult;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.Restrictions;
import com.hengpeng.itfinbase.en.ActivityStatus;
import com.hengpeng.itfinbase.en.ActivityType;
import com.hengpeng.itfinbase.en.CustomerType;
import com.hengpeng.itfinbase.en.PlatForm;
import com.hengpeng.itfinbase.persist.Activity;
import com.hengpeng.itfinbase.persist.Reward;
import com.hengpeng.itfinoss.service.ServiceFactory;
import com.hengpeng.itfinoss.service.activity.ActivityService;

@Service
@Lazy
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ServiceFactory serviceFactory;

    @Override
    public ServiceResult<List<Activity>> selectActivityPage(int indexPage, int pageSize) {

        DynamicQuery query = new DynamicQuery(Activity.class);
        query.setIsSearchTotalCount(true);
        query.setPageIndex(indexPage);
        query.setPageSize(pageSize);
        query.desc("id");
        QueryResult<Activity> requery = serviceFactory.getPersistService().findWithPage(Activity.class, query);
        ServiceResult<List<Activity>> result = new ServiceResult<List<Activity>>(requery.getResults());
        result.setReturnListSize((int) requery.getTotalCount());
        return result;
    }

    @Override
    public List<Reward> getRewardsByActivityId(Long id) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Activity getActivityById() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Activity modify(Long id, String activityName, ActivityStatus status, CustomerType customerType,
            PlatForm platform, Date startDate, Date endDate, ActivityType activityType, Set<Reward> rewards) {
        Activity activity = serviceFactory.getPersistService().get(Activity.class, id);

        if (activity == null) {
            throw new ResourceNotFoundException(id);
        }
        if (activityName != null) {
            activity.setActivityName(activityName);
        }
        if (status != null) {
            activity.setStatus(status);
        }
        if (customerType != null) {
            activity.setCustomerType(customerType);
        }
        if (platform != null) {
            activity.setPlatform(platform);
        }
        if (startDate != null) {
            activity.setStartDate(startDate);
        }
        if (endDate != null) {
            activity.setEndDate(endDate);
        }
        if (activityType != null) {
            activity.setActivityType(activityType);
        }
        if (rewards != null) {
            activity.setRewards(rewards);
        }
        Date date = new Date();
        activity.setUpdateTime(date);
        return serviceFactory.getPersistService().update(activity);
    }

    public List<Reward> findRewardByIds(Long[] ids) {
        DynamicQuery query = new DynamicQuery(Reward.class);
        query.add(Restrictions.inArray("id", ids));
        return serviceFactory.getPersistService().find(Reward.class, query);
    }

    @Override
    public Activity save(Activity activity) {

        return serviceFactory.getPersistService().save(activity);
    }

}
